<?php

use Illuminate\Database\Capsule\Manager as DB;

#[返佣月账单统计]
class MonthfanyongController extends AcommonController
{

    public function init()
    {

        $this->model      = new fanyongmonthModel();
        $this->table      = 'fanyong_month';
        $this->primaryKey = 'id';
        parent::init();
    }


    /**
     * @name 获取数据列表_基类
     * @param bool $returnData
     * @return array
     */
    #[获取数据列表_基类]
    public function indexAction($returnData = FALSE)
    {
        if ($this->isPost()) {
            $startOn = $this->get('start_on');
            if (!empty($startOn)) {
                $this->model = $this->model->whereDate('created_at', '>=', $startOn);
            }
            $endOn = $this->get('end_on');
            if (!empty($endOn)) {
                $this->model = $this->model->whereDate('created_at', '<=', $endOn);
            }
            parent::indexAction(false);
        } else {

            $this->_view->display( strtolower($this->controller) . '/index.html');
        }
    }




    #[储备金列表]
    public function rechargeuserlistAction()
    {
        $info = DB::table('order_bonus_remaining')->first();
        if ($this->isPost()) {
            $this->model = (new orderbonusreserveModel());
            $startOn = $this->get('start_on');
            if (!empty($startOn)) {
                $this->model = $this->model->whereDate('created_at', '>=', $startOn);
            }
            $endOn = $this->get('end_on');
            if (!empty($endOn)) {
                $this->model = $this->model->whereDate('created_at', '<=', $endOn);
            }
            $status = $this->get('status');
            if (!empty($status)) {
                if($status=='deleted'){
                    $this->model = $this->model->whereNotNull('deleted_at');
                }else{
                    $this->model = $this->model->whereNull('deleted_at');
                    array_push($this->condition, ['status', '=', $status]);
                }
            }
            parent::indexAction(false);
        } else {

            $this->_view->assign('zong', $info->standby_money);
            $this->_view->display( strtolower($this->controller) . '/rechargeuserlist.html');
        }
    }

    #[返利金列表]
    public function rebatelistAction()
    {
        $info = DB::table('order_bonus_remaining')->first();
        if ($this->isPost()) {
            $this->model = (new orderbonusrebateModel());

            $startOn = $this->get('start_on');
            if (!empty($startOn)) {
                $this->model = $this->model->whereDate('created_at', '>=', $startOn);
            }
            $endOn = $this->get('end_on');
            if (!empty($endOn)) {
                $this->model = $this->model->whereDate('created_at', '<=', $endOn);
            }
            $status = $this->get('status');
            if (!empty($status)) {
                if($status=='deleted'){
                    $this->model = $this->model->whereNotNull('deleted_at');
                }else{
                    $this->model = $this->model->whereNull('deleted_at');
                    array_push($this->condition, ['status', '=', $status]);
                }
            }
            parent::indexAction(false);
        } else {
            $this->_view->assign('zong', $info->money);
            $this->_view->display( strtolower($this->controller) . '/rebatelist.html');
        }
    }

    #[返利金转入]
    public function rechargeAction()
    {
        $info = DB::table('order_bonus_remaining')->first();
        if ($this->isPost()) {
            $rows  = $this->postData;
            $roles = [
                'money'     => ['role' => "required|neq:0", 'msg' => '金额'],
            ];
            Validate::check($rows, $roles);

            try {
                DB::beginTransaction();
                $money = $info->money + $rows['money'];
                $standby_money = $info->standby_money - $rows['money'];
                DB::table('order_bonus_remaining')->update(['money'=>$money,'standby_money'=>$standby_money]);

                $tow_arr['money']      = $rows['money'];
                $tow_arr['memo']       = "管理员转出";
                $tow_arr['created_at'] = date('Y-m-d H:i:s');
                DB::table('order_bonus_reserve')->insertGetId($tow_arr);

                $tow_arr['memo']       = "管理员转入";
                DB::table('order_bonus_rebate')->insertGetId($tow_arr);

                DB::commit();
                $result = [
                    'ret'  => 0,
                    'msg'  => '操作成功',
                    'data' => []
                ];

            } catch (Exception $e) {
                DB::rollback();
                $result = [
                    'ret' => 30001,
                    'msg' => $e->getMessage(),
                ];
            }
            json($result);
        } else {

            $this->_view->assign('curr_money', $info->standby_money);
            $this->_view->display(strtolower($this->controller) . '/recharge.html');
        }
    }

}
